package labox.innovation.gameserver.pathfinding.utils;

import java.util.LinkedList;

import labox.innovation.config.Config;
import labox.innovation.gameserver.pathfinding.Node;

/**
 * ������ʵ�ֵ�һ���򵥵�������Ҳ������arraylist,ʱ�临�Ӷ�O(n)
 * 
 * @author -squll-
 */
public final class FastNodeList {

	private static final LinkedList<FastNodeList> _buff = new LinkedList<FastNodeList>();

	private Node[] _list;

	private int _size;

	public static final FastNodeList newInstance() {
		FastNodeList list = _buff.poll();
		if (list == null)
			return new FastNodeList(Config.PATHFINDING_MAX_NODE);
		list._size = 0;
		return list;
	}

	public static final void recycle(FastNodeList recycle) {
		if (recycle != null)
			_buff.add(recycle);
	}

	private FastNodeList(int size) {
		_list = new Node[size];
	}

	public boolean add(Node n) {
		if (_size >= 800)
			return false;
		_list[_size++] = n;
		return true;
	}

	public boolean contains(Node n) {
		for (int i = -1; ++i < _size;)
			if (_list[i].equals(n))
				return true;
		return false;
	}

	public boolean containsRev(Node n) {
		for (int i = _size; --i >= 0;)
			if (_list[i].equals(n))
				return true;
		return false;
	}
}
